import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;



public class ZDialog extends JDialog {
private ZDialogInfo zInfo = new ZDialogInfo();
private boolean sendData;
private JLabel freqLabel, nbcanLabel,nboctLabel;
private JTextField freq, nbcan, nboct;

public ZDialog(JFrame parent, String title, boolean modal){
super(parent, title, modal);
this.setSize(550, 270);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.initComponent();
}
public ZDialogInfo showZDialog(){
this.sendData = false;
this.setVisible(true);
return this.zInfo;
}
private void initComponent(){

	//La Frequence
	JPanel panFreqEchan = new JPanel();
	panFreqEchan.setBackground(Color.white);
	panFreqEchan.setPreferredSize(new Dimension(220, 60));
	freq = new JTextField();
	freq.setPreferredSize(new Dimension(50, 25));
	freq.addKeyListener(new ClavierListener());
	panFreqEchan.add(freq);
	panFreqEchan.setBorder(BorderFactory.createTitledBorder("Fréquence d'échantillonage"));
	freqLabel = new JLabel("Saisir une valeur");
	panFreqEchan.add(freqLabel);
	

	//Le Nbcan
	JPanel panNbcan = new JPanel();
	panNbcan.setBackground(Color.white);
	panNbcan.setPreferredSize(new Dimension(220, 60));
	nbcan = new JTextField();
	nbcan.setPreferredSize(new Dimension(50, 25));
	nbcan.addKeyListener(new ClavierListener());
	panNbcan.add(nbcan);
	panNbcan.setBorder(BorderFactory.createTitledBorder("Nombre de canaux"));
	nbcanLabel = new JLabel("Saisir une valeur");
	panNbcan.add(nbcanLabel);
	
	

	//Le nboct
	JPanel panNboct = new JPanel();
	panNboct.setBackground(Color.white);
	panNboct.setPreferredSize(new Dimension(220, 60));
	panNboct.setBorder(BorderFactory.createTitledBorder("Nombre d'octets /Echant"));
	nboctLabel = new JLabel("Saisir une valeur : ");
	nboct = new JTextField();
	nboct.addKeyListener(new ClavierListener());
	nboct.setPreferredSize(new Dimension(50, 25));
	panNboct.add(nboctLabel);
	panNboct.add(nboct);
	

	JPanel content = new JPanel();
	content.setBackground(Color.white);
	content.add(panFreqEchan);
	content.add(panNbcan);
	content.add(panNboct);
	JPanel control = new JPanel();
	JButton okBouton = new JButton("OK");
	okBouton.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		zInfo = new ZDialogInfo(freq.getText(),nbcan.getText(),nboct.getText());
		setVisible(false);
		}
	});
	JButton cancelBouton = new JButton("Annuler");
	cancelBouton.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent arg0) {
	setVisible(false);
	}
	});
	control.add(okBouton);
	control.add(cancelBouton);
	
	this.getContentPane().add(content, BorderLayout.CENTER);
	this.getContentPane().add(control, BorderLayout.SOUTH);
}
	
	class ClavierListener implements KeyListener{
		public void keyReleased(KeyEvent event) {
		if(!isNumeric(event.getKeyChar()))
		freq.setText(freq.getText().replace(String.valueOf(event.getKeyChar()),
		""));
		if(!isNumeric(event.getKeyChar()))
		nbcan.setText(nbcan.getText().replace(String.valueOf(event.getKeyChar()),
				""));
		if(!isNumeric(event.getKeyChar()))
		nboct.setText(nboct.getText().replace(String.valueOf(event.getKeyChar()),
				""));
		}

		//Inutile de redéfinir ces méthodes, ous n'en avons plus l'utilité !
		public void keyPressed(KeyEvent event) {}
		public void keyTyped(KeyEvent event) {}
		//Retourne true si le paramètre est numérique, false dans le cas contraire
		private boolean isNumeric(char carac){
		try {
		Integer.parseInt(String.valueOf(carac));
		}
		catch (NumberFormatException e) {
		return false;
		}
		return true;
		}
		}
}

	
	

